/**
 * 
 */
package com.yidu.ssm.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.yidu.common.Pages;
import com.yidu.common.Tools;
import com.yidu.ssm.domain.Flower;
import com.yidu.ssm.service.FlowerService;

/**
 * Flower与前端交互的Action
 * @author liandyao
 * @date 2018年7月5日
 * @version 1.0
 */
@Controller
@RequestMapping("/flower")
public class FlowerAction {
	@Resource
	private FlowerService flowerService;

    /**
    * 得到所有的花朵集合
    */
	@RequestMapping("selectAll")
	@ResponseBody
	public List<Flower> selectAll(FlowerVo vo){
		return flowerService.selectAll(vo);
	}
	
	/**
	 * 得到分页的数据
	 * @param vo
	 * @return
	 */
	@RequestMapping("getPages")
	@ResponseBody
	public Pages getPages(FlowerVo vo){
		Pages p = new Pages();
		int count = flowerService.selectCount(vo);
		p.setTotalRows(count); 
		return p ;
	}
	
	/**
	 * 增加或者修改
	 * @param file
	 * @param vo
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("addOrUpdate")
	public ModelAndView addOrUpdate(
				@RequestParam(value = "pic", required = false) 
				MultipartFile file,
				FlowerVo vo,
				HttpServletRequest request) throws IllegalStateException, IOException{
		ModelAndView mv = new ModelAndView();
		//将图片保存到upload文件夹,并且返回一个新的文件地址
		String path = Tools.getRealPath(request, file);
		
		vo.setFlImage(path);
		
		flowerService.addOrUpdate(vo);
		
		mv.setViewName("redirect:/pages/flower/flowerList.html");
		return mv ;
		
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("delete") 
	public String getPages(String id){
		 
		flowerService.deleteByPrimaryKey(Integer.valueOf(id));
		
		return "redirect:/pages/flower/flowerList.html" ;
	}
	
	/**
	 * 根据ID找到flower对象
	 * @return
	 */
	@RequestMapping("findById") 
	@ResponseBody
	public FlowerVo findById(String id){
 
		FlowerVo vo = flowerService.selectByPrimaryKey(Integer.valueOf(id));
		
		return vo ;
	}
}
